package cci.demo;

import java.util.Locale;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

@ManagedBean
@SessionScoped
public class FormSettings {
	
	private boolean isFrench = true;
	private final Locale FRENCH = Locale.FRENCH;
	private final Locale AMERICAN = new Locale("us");

	public Locale getLocale() {
		if (isFrench) {
			return(FRENCH);
		} else {
			return(AMERICAN);
		}
	}

	private void switchLocale() {
		isFrench = !isFrench;
		Locale newLocale;
		if (isFrench) {
			newLocale = FRENCH;
		} else {
			newLocale = AMERICAN;
		}
		FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);
	}

	public void swapLocale(ActionEvent event) {
		switchLocale();
	}
	
	public void swapLocale2(ValueChangeEvent event) {
		Boolean flag = (Boolean)event.getNewValue();
		if (flag) {
	    		switchLocale(); 
		} 
	} 
}
